using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using SharpCore.Data;
using SharpCore.Utilities;

namespace Dating.DA
{
	public class PersonalityQuestionDA
	{
		#region Fields

		protected string connectionStringName;

		#endregion

		#region Constructors
		public PersonalityQuestionDA()
		{
		}

		public PersonalityQuestionDA(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the PersonalityQuestion table.
		/// </summary>
		public  int Insert(PersonalityQuestion personalityQuestion)
		{
			ValidationUtility.ValidateArgument("personalityQuestion", personalityQuestion);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", personalityQuestion.TenantID),
				new SqlParameter("@Question", personalityQuestion.Question),
				new SqlParameter("@Order", personalityQuestion.Order)
			};

			personalityQuestion.QuestionID = Convert.ToInt32(SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "PersonalityQuestionInsert", parameters));
			return personalityQuestion.QuestionID;
		}

		/// <summary>
		/// Updates a record in the PersonalityQuestion table.
		/// </summary>
		public  int Update(PersonalityQuestion personalityQuestion)
		{
			ValidationUtility.ValidateArgument("personalityQuestion", personalityQuestion);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@QuestionID", personalityQuestion.QuestionID),
				new SqlParameter("@TenantID", personalityQuestion.TenantID),
				new SqlParameter("@Question", personalityQuestion.Question),
				new SqlParameter("@Order", personalityQuestion.Order)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PersonalityQuestionUpdate", parameters));
		}

		/// <summary>
		/// Deletes a record from the PersonalityQuestion table by its primary key.
		/// </summary>
		public  int Delete(int questionID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@QuestionID", questionID)
,				new SqlParameter("@TenantID", tenantID)
			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PersonalityQuestionDelete", parameters));
		}

		/// <summary>
		/// Deletes a record from the PersonalityQuestion table by a foreign key.
		/// </summary>
		public  int DeleteAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			return(SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "PersonalityQuestionDeleteAllByTenantID", parameters));
		}

		/// <summary>
		/// Selects a single record from the PersonalityQuestion table.
		/// </summary>
		public  PersonalityQuestion Select(int questionID, int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@QuestionID", questionID)
,				new SqlParameter("@TenantID", tenantID)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PersonalityQuestionSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MakePersonalityQuestion(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the PersonalityQuestion table.
		/// </summary>
		public  List<PersonalityQuestion> SelectAll(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)
			};
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PersonalityQuestionSelectAll", parameters))
			{
				List<PersonalityQuestion> personalityQuestionList = new List<PersonalityQuestion>();
				while (dataReader.Read())
				{
					PersonalityQuestion personalityQuestion = MakePersonalityQuestion(dataReader);
					personalityQuestionList.Add(personalityQuestion);
				}

				return personalityQuestionList;
			}
		}

		/// <summary>
		/// Selects all records from the PersonalityQuestion table by a foreign key.
		/// </summary>
		public  List<PersonalityQuestion> SelectAllByTenantID(int tenantID)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@TenantID", tenantID)

			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "PersonalityQuestionSelectAllByTenantID", parameters))
			{
				List<PersonalityQuestion> personalityQuestionList = new List<PersonalityQuestion>();
				while (dataReader.Read())
				{
					PersonalityQuestion personalityQuestion = MakePersonalityQuestion(dataReader);
					personalityQuestionList.Add(personalityQuestion);
				}

				return personalityQuestionList;
			}
		}

		/// <summary>
		/// Creates a new instance of the PersonalityQuestion class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected  PersonalityQuestion MakePersonalityQuestion(SqlDataReader dataReader)
		{
			PersonalityQuestion personalityQuestion = new PersonalityQuestion();
			personalityQuestion.QuestionID = SqlClientUtility.GetInt32(dataReader, "QuestionID", 0);
			personalityQuestion.TenantID = SqlClientUtility.GetInt32(dataReader, "TenantID", 0);
			personalityQuestion.Question = SqlClientUtility.GetString(dataReader, "Question", String.Empty);
			personalityQuestion.Order = SqlClientUtility.GetInt32(dataReader, "Order", 0);

			return personalityQuestion;
		}

		#endregion
	}
}
